package com.ljf.common.web;

import com.ljf.common.dto.query.PageParam;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.result.ResultModel;
import com.ljf.common.service.IBasisService;
import com.ljf.common.validation.AddGroup;
import com.ljf.common.validation.QueryGroup;
import com.ljf.common.validation.UpdateGroup;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Arrays;

/**
 * 基础控制器
 *
 * @author LJF
 * @since 2024-01-23
 */
public class BasisController<S extends IBasisService<Q, V, A>, Q extends PageParam, V extends Serializable, A extends Serializable> extends NoDBBasisController<S> {

    @PostMapping
    @ApiOperation(value = "新增接口")
    public ResultModel add(@RequestBody @Validated(AddGroup.class) A command) throws ServiceException {
        return service.add(command);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public ResultModel delete(@PathVariable Long id) throws ServiceException {
        return service.delete(id);
    }

    @DeleteMapping("/batch")
    @ApiOperation(value = "批量删除接口")
    public ResultModel deleteBatch(@RequestBody Long[] ids) throws ServiceException {
        return service.deleteBatch(Arrays.asList(ids));
    }

    @PutMapping
    @ApiOperation(value = "更新接口")
    public ResultModel edit(@RequestBody @Validated(UpdateGroup.class) A command) throws ServiceException {
        return service.edit(command);
    }

    @GetMapping
    @ApiOperation(value = "分页查询接口")
    public ResultModel query(@Validated(QueryGroup.class) Q query) throws ServiceException {
        return service.query(query);
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询接口")
    public ResultModel queryList(@Validated(QueryGroup.class) Q query) throws ServiceException {
        return service.queryList(query);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询接口")
    public ResultModel queryById(@PathVariable Long id) throws ServiceException {
        return service.queryById(id);
    }

    @GetMapping("/one")
    @ApiOperation(value = "查询接口(仅一条数据)")
    public ResultModel queryOne(@Validated(QueryGroup.class) Q query) throws ServiceException {
        return service.queryOne(query);
    }

}
